package com.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.bean.*;
import com.mall.dao.RoleMapper;
import com.mall.dao.UserMapper;
import com.mall.dao.UserRoleMapper;
import com.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author lwd1513
 * @date 2022-04-15 16 44
 * discription
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public int insert(User user) {
        user.setCreated(new Date());
        user.setUpdated(new Date());
        user.setFlag(1);
        if (user.getFile() == null) {
            user.setFile("http://lwd520.oss-cn-zhangjiakou.aliyuncs.com/20220402/477ae4ee1fa54d558c869a4c4fe792ac.jpeg");
        }

        int res = userMapper.insertSelective(user);
        return res;
    }

    @Override
    public List<User> queryData(User user) {
        return null;
    }

    @Override
    public int update(User user) {
        return 0;
    }

    @Override
    public int deleteById(String id) {

        int res = userMapper.deleteByPrimaryKey(Integer.parseInt(id));

        int res2 = userRoleMapper.deleteByUserId(Integer.parseInt(id));
        return 0;
    }

    @Override
    public User queryDataById(String id) {
        return null;
    }

    @Override
    public PageInfo<User> queryDataByPage(int currentPage, int pageSize, User user) {
        PageHelper.startPage(currentPage, pageSize);
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andFlagEqualTo(1);
        if (user.getUsername() != null && !user.getUsername().equals("")) {
            criteria.andUsernameLike("%" + user.getUsername() + "%");
        }
        List<User> users = userMapper.selectByExample(userExample);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public User userLoginByUserName(String name, String password) {

        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();

        criteria.andUsernameEqualTo(name);
        criteria.andPasswordEqualTo(password);

        List<User> users = userMapper.selectByExample(userExample);

        if (users.size() > 0) {
            User userLogin = users.get(0);
            if (userLogin.getUser_id() != null) {

                Role role = roleMapper.getRoleByUserRoleUserId(String.valueOf(userLogin.getUser_id()));
                //设置给user对象
                userLogin.setRole(role);

            }
            return userLogin;
        }

        return null;
    }

    @Override
    public int updateUserStateExChange(int flag, int userId) {
        if (flag != 0) {
            flag = 0;
        } else {
            flag = 1;
        }
        User user = new User();
        user.setFlag(flag);
        user.setUser_id(userId);
        int res = userMapper.updateByPrimaryKeySelective(user);
        return res;
    }

    @Override
    public int updatePathByUserId(String file, String username) {
        int res = userMapper.updatePathByUsername(file, username);
        return res;
    }

    @Override
    public int updateUserById(User user) {

        user.setUpdated(new Date());
        int res = userMapper.updateByPrimaryKeySelective(user);
        return 0;
    }

    @Override
    public int assignRoleById(UserRole userRole) {

        UserRoleExample example = new UserRoleExample();
        UserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUser_idEqualTo(userRole.getUser_id());
        List<UserRole> userRoles = userRoleMapper.selectByExample(example);

        if (userRoles.size() > 0) {
            //说明之前已经分配过权限
            UserRole userRole1 = userRoles.get(0);
            userRole1.setRole_id(userRole.getRole_id());
            int res = userRoleMapper.updateByPrimaryKeySelective(userRole1);
        } else {
            int res = userRoleMapper.insert(userRole);
        }
        return 1;
    }

}
